//cfgremoteexec的class Functions设置为mode=0或1，则永远不会执行脚本initPlayerServer.sqf
//JIP玩家传送到队友身边

private ["_player","_players","_placed"];

_player = _this select 0;

diag_log format["initPlayerServer run for %1", name _player];

if(name _player == "HC") then {
    ["HC present. Offloading AI."] spawn a3e_fnc_debugmsg;
    A3E_HC_ID = owner _player;
    if(A3E_HC_ID==0) exitwith {
        ["Getting HC ID failed!"] spawn a3e_fnc_debugmsg;
    };
} else {
    //[format["%1 joined the Game!",name _player]] spawn a3e_fnc_debugmsg;
	/*if(!isNil("paramsArray")) then {
		paramsArray call A3E_fnc_WriteParamBriefing;
	};*/

	
	//The following is outdates. We are now using "allplayers". But this might be referenced somewhere
	if (isNil "A3E_Players") then {A3E_Players = []};
    A3E_Players = A3E_Players + [_player];
    publicVariable "A3E_Players";
	

	_player addEventHandler ["HandleScore","_this call A3E_FNC_handleScore"]; //得分处理
	
	[_player, true] remoteExec ["setCaptive", _player];
	diag_log format["Escape debug: %1 is waiting for prison creation.", name _player];

	//Wait until most of the mission is loaded and the player is locally ready (means no weapons etc)
    waituntil{sleep .5;(!isNil("A3E_FenceIsCreated") && !isNil("A3E_StartPos") && !isNil("A3E_ParamsParsed")) && (_player getvariable["A3E_PlayerInitializedLocal",false])};
	diag_log format["Escape debug: Player %1 is ready and will be placed by the server.", name _player];
	_placed = false;
	if (time > 60) then {
		_players = call A3E_fnc_GetPlayers;
		if (count _players > 2) then {_players deleteAt (_players find Player_Server)};
		if (!isNil "Escape_Players_enemy" && {count Escape_Players_enemy > 0}) then {_players = _players - Escape_Players_enemy}; //
		_players deleteAt (_players find _player);
		{if(_x getVariable ["FAR_is_killFriend",false])then{_players = _players - [_x]}} forEach _players;
		while {count _players > 0 && {!_placed}} do {
			private _refPlayer = selectRandom _players;
			private _refVehicle = vehicle _refPlayer;
			if (_refVehicle != _refPlayer && {_refVehicle emptyPositions "Commander">0 || {_refVehicle emptyPositions "Driver">0} || {_refVehicle emptyPositions "Gunner">0} || {_refVehicle emptyPositions "Cargo">0}}) then {
					//_player moveInAny _refVehicle;
					//Teleports the player remotely into the Vehicle, needs to be called local at player
					[_player,_refVehicle] remoteExec ["moveInAny", _player]; 
					sleep .5;
					_placed = if(_player in _refVehicle)then[{true},{false}];
				} else {
					_player setPosASL [(getPosASL _refPlayer select 0) + (random 4) - 2, (getPosASL _refPlayer select 1) + (random 6) - 3, getPosASL _refPlayer select 2]; //
					_placed = true;
				};
			//Remove the player to test another one
			_players deleteAt (_players find _refPlayer);
		};
	};
	if (!_placed) then {
		_player setpos [(A3E_StartPos select 0)+random 3-1.5,(A3E_StartPos select 1)+random 3-1.5,0];
		_player setdir (random 360);
		diag_log format["Escape debug: %1 placed at prison.", name _player];
	}; //监狱位置
	sleep .5;
	diag_log format["Escape debug: %1 is now ready (serverside).", name _player];

	//A3E_PlayerInitializedServer means the player was placed in Prison and is ready for the "Intro"
	_player setvariable["A3E_PlayerInitializedServer",true,true];
};

